package com.reach.redis.interceptor;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;

import com.reach.redis.annotation.CacheEvict;
import com.reach.redis.annotation.Cacheable;
import com.reach.redis.bo.CacheConfig;
import com.reach.redis.manager.ManagerCache;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

 
/**
 * 缓存拦截
 */

public class CacheInterceptor extends AbstractCacheInterceptor implements MethodInterceptor  {
	
	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		if(ManagerCache.enableCache) {
			CacheConfig cacheConfig = ManagerCache.queryCacheMap.get(getClassName(obj.getClass().getName()) +"." +method.getName());
			if(cacheConfig.getCache() instanceof Cacheable) {
				return new QueryCacheInterceptor().intercept(obj, method, args, proxy);
			}else if(cacheConfig.getCache() instanceof CacheEvict) {
				return new ModificationCacheInterceptor().intercept(obj, method, args, proxy);
			}
		}
	
		return proxy.invokeSuper(obj, args);
	}

	@Override
	public String[] getAnnotation(CacheConfig cacheConfig) {
		Cacheable cacheable = (Cacheable) cacheConfig.getCache();
		
		return cacheable.key().split(",");
	}

	@Override
	public String getModule(CacheConfig cacheConfig) {
		return null;
	}

	@Override
	public String getEvent(CacheConfig cacheConfig) {
		return null;
	}
 
}